package io.persimmon.core.dto.factory;

import io.persimmon.core.dto.PageData;
import io.persimmon.core.exception.PersimmonExceptionFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PageDataFactory {
    public static <T> PageData<T> create(Collection<T> list, int pageNum, int pageSize) {
        PersimmonExceptionFactory.assertIf(pageNum <= 0, "pageNum must be >= 0");
        int start = (pageNum - 1) * pageSize;
        if (start > list.size()) {
            return new PageData<>(list.size(), new ArrayList<>());
        }
        int end = Math.min(start + pageSize, list.size());
        List<T> result = new ArrayList<>(list).subList(start, end);

        return new PageData<T>(list.size(), result);
    }
}
